#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <cassert>
#include <cstdlib>
#include "../comm/log.hpp"
#include "../comm/util.hpp"

// model：主要用来和数据进行交互, 对外提供访问数据的接口

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
    class Question
    {
    public:
    Question()
    {}
    ~Question()
    {}
    public:
        std::string number; // 题目编号，唯一
        std::string title;  // 题目标题
        std::string star;   // 难度
        int cpu_limit;      // 时间限制
        int mem_limit;      // 空间限制
        std::string desc;   // 题目描述
        std::string header; // 题目预设给用户在线编辑器的代码
        std::string tail;   // 题目的测试用例，需要和 header 拼接，形成完成代码
    };
    const std::string questions_list = "./question/questions.list";
    const std::string questions_path = "./question/";
    class Model
    {
    private:
        unordered_map<string, Question> _questions; // 题号->题目细节
    public:
        Model()
        {
            assert(LoadQuestionList(questions_list));
        }
        bool LoadQuestionList(const std::string &question_list)
        {
            // 加载配置文件：questions/questions.list + 题目文件编号
            ifstream in(questions_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件" << "\n";
                return false;
            }
            string line;
            while(getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                // 1 判断回文数 简单 1 256
                if(tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，请检查文件格式" << "\n";
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                string path = questions_path;
                path += q.number;
                path += "/";
                FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "header.cpp", &(q.header), true);
                FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);
                
                _questions.insert({q.number, q});
            }
            LOG(INFO) << "加载题库...成功" << "\n"; 
            in.close();

            return true;
        }
        bool GetAllQuestions(vector<Question> *out) // 获取所有题目
        {
            if (_questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for (const auto &q : _questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetOneQuestion(const std::string &number, Question *q) // 获取某个题目
        {
            const auto &iter = _questions.find(number);
            if (iter == _questions.end()) // 没找到 
            {
                LOG(ERROR) << "用户获取题目 " << number << " 失败" << "\n";
                return false;
            }
            (*q) = iter->second;
            return true;
        }
        ~Model()
        {}
    };
}